package com.gitee.feizns.quickstart.easyexcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.handler.WriteHandler;
import com.gitee.feizns.quickstart.easyexcel.impl.SheetReaderListener;
import com.gitee.feizns.quickstart.easyexcel.style.Styles;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * {@link EasyExcel}
 * @author feizns
 * @since 2025/4/18
 */
public class EasyExcels {

    /**
     * 默认宽度
     */
    public static final int DefaultWidth = 3500;

    /**
     * 读
     * @param is {@link InputStream}
     * @return {@link Sheet }
     */
    public static Sheet read(InputStream is) {
        SheetReaderListener listener = new SheetReaderListener();
        EasyExcel.read(is, listener).sheet().doRead();
        return listener.getSheet();
    }

    /**
     * 读
     * @param is {@link InputStream}
     * @param head 头
     * @return {@link Sheet }
     */
    public static <T> List<T> read(InputStream is, Class<T> head) {
        return EasyExcel.read(is).head(head).sheet().doReadSync();
    }

    /**
     * 写入
     * @param os 输出流
     * @param head 头
     * @param data 数据
     * @param handlers 处理器
     */
    public static <T> void write(OutputStream os, Class<T> head, List<T> data, WriteHandler... handlers) {
        ExcelWriterBuilder builder = EasyExcel.write(os).head(head);
        registerWriteHandler(builder, handlers).sheet().doWrite(data);
    }

    /**
     * 写
     * @param os 输出流
     * @param head 头
     * @param handlers 处理器
     */
    public static <T> void write(OutputStream os, Class<T> head, WriteHandler... handlers) {
        write(os, head, new ArrayList<>(), handlers);
    }

    /**
     * 写入
     * @param heads 头
     * @param data 数据
     */
    public static void write(OutputStream os, List<String> heads, List<List<Object>> data, WriteHandler... handlers) {
        ExcelWriterBuilder builder = EasyExcel.write(os).head(heads.stream().map(Collections::singletonList).collect(Collectors.toList()));
        registerWriteHandler(builder, handlers).sheet().doWrite(data);
    }

    /**
     * 写入
     * @param os {@link OutputStream}
     * @param heads 头
     */
    public static void write(OutputStream os, List<String> heads, WriteHandler... handlers) {
        write(os, heads, new ArrayList<>(), handlers);
    }

    /**
     * build 默认设置
     * @param builder 建筑工人
     * @param handlers 处理器
     * @return {@link ExcelWriterBuilder }
     */
    private static ExcelWriterBuilder registerWriteHandler(ExcelWriterBuilder builder, WriteHandler... handlers) {
        builder.registerWriteHandler(Styles.defaultHorizontalCellStyleStrategy());
        builder.registerWriteHandler(Styles.setWidth(DefaultWidth));
        Arrays.stream(handlers).forEach(builder::registerWriteHandler);
        return builder;
    }

}
